package com.ny.service.impl;

import com.ny.mapper.ComplaintMapper;
import com.ny.mapper.MemberMapper;
import com.ny.domain.ComplaintDO;
import com.ny.dto.ComplaintDTO;
import com.ny.service.ComplaintService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Override;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import com.ny.domain.MemberDO;
import java.util.Date;

/**
 * 投诉信息管理的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ComplaintMapper complaintMapper;

    @Override
    public Boolean addComplaint(ComplaintDTO complaintDTO) {
        MemberDO memberDO = memberMapper.selectById(complaintDTO.getMemberId());
        if (memberDO == null) {
            throw new BusinessException("会员不存在，无法添加投诉信息");
        }
        ComplaintDO complaintDO = new ComplaintDO();
        complaintDO.setMemberId(complaintDTO.getMemberId());
        complaintDO.setComplaintDescription(complaintDTO.getComplaintDescription());
        complaintDO.setComplaintDate(new Date());
        // 默认状态为pending
        complaintDO.setStatus("pending");
        complaintDO.setCreateBy("system");
        complaintDO.setCreateTime(new Date());
        complaintDO.setUpdateBy("system");
        complaintDO.setUpdateTime(new Date());
        int result = complaintMapper.insert(complaintDO);
        return result > 0;
    }

    @Override
    public Boolean deleteComplaint(ComplaintDTO complaintDTO) {
        ComplaintDO complaintDO = complaintMapper.selectById(complaintDTO.getComplaintId());
        if (complaintDO == null) {
            throw new BusinessException("投诉信息不存在，无法删除");
        }
        int result = complaintMapper.deleteById(complaintDTO.getComplaintId());
        return result > 0;
    }

    @Override
    public Boolean updateComplaintStatus(ComplaintDTO complaintDTO) {
        ComplaintDO complaintDO = complaintMapper.selectById(complaintDTO.getComplaintId());
        if (complaintDO == null) {
            throw new BusinessException("投诉信息不存在，无法更新状态");
        }
        complaintDO.setStatus(complaintDTO.getStatus());
        complaintDO.setUpdateBy("system");
        complaintDO.setUpdateTime(new Date());
        int result = complaintMapper.updateById(complaintDO);
        return result > 0;
    }

    @Override
    public ComplaintDO getComplaintInfo(Integer memberId) {
        MemberDO memberDO = memberMapper.selectById(memberId);
        if (memberDO == null) {
            throw new BusinessException("会员不存在，无法获取投诉信息");
        }
        ComplaintDO complaintDO = complaintMapper.selectByMemberId(memberId);
        return complaintDO;
    }

    @Override
    public List<ComplaintDO> getAllComplaintInfos() {
        List<ComplaintDO> complaintDOList = complaintMapper.selectAll();
        return complaintDOList;
    }
}
